/**
    <one line to give the program's name and a brief idea of what it does.>
    Copyright (C) 2010  Sean Beecroft

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

 * @version 1.10 2010-01-01
 * @author Sean Beecroft
 */

/* (swing1.1) */ 
package yawiki.client.gui;


import java.util.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.plaf.metal.*;


/**
 * @version 1.0 01/12/99
 */
public class YTextIcons extends MetalIconFactory.TreeLeafIcon {

    protected String label;
    private static Hashtable labels;
  
    protected YTextIcons() {}

    public void paintIcon(Component c, Graphics g, int x, int y) {
        super.paintIcon(c, g, x, y);
        if (label != null) {
            FontMetrics fm = g.getFontMetrics();
          
            int offsetX = (getIconWidth() - fm.stringWidth(label)) / 2;
            int offsetY = (getIconHeight() - fm.getHeight()) / 2 - 2;
      
            g.drawString(label, x + offsetX, y + offsetY + fm.getHeight());
        }
    }
  
    public static Icon getIcon(String str) {
        if (labels == null) {
            labels = new Hashtable();
            setDefaultSet();
        }
        YTextIcons icon = new YTextIcons();

        icon.label = (String) labels.get(str);
        return icon;
    }
  
    public static void setLabelSet(String ext, String label) {
        if (labels == null) {
            labels = new Hashtable();
            setDefaultSet();
        }
        labels.put(ext, label);   
    }
  
    private static void setDefaultSet() {
        labels.put("c", "C");   
        labels.put("java", "J");   
        labels.put("html", "H");   
        labels.put("htm", "H");   
    
        // and so on
        labels.put("txt", "TXT");   
        labels.put("TXT", "TXT");   
        labels.put("cc", "C++");   
        labels.put("C", "C++");   
        labels.put("cpp", "C++");   
        labels.put("exe", "BIN");   
        labels.put("class", "BIN");   
        labels.put("gif", "GIF");   
        labels.put("GIF", "GIF");  
    
        labels.put("", "");   
    
    }

}
